<style>
	@import url("../../../common/css/address.css");
</style>
<template>
	<view>
		<commonHeader :titleName="titleName"></commonHeader>
		<view class="content_main">
			<view class="address_top">
				<view class="main_item">
					<view class="address_title">
						联系人
					</view>
					<view class="address_content_user">
						<input type="text" placeholder="请输入联系人" v-model="username" />
					</view>
					<view class="address_sex">
						<span :class="[sex == 1 ? 'active' : '']" @click="chooseSex(1)">先生</span>
						<span :class="[sex == 0 ? 'active' : '']" @click="chooseSex(0)">女士</span>
					</view>
				</view>
				<view class="main_item">
					<view class="address_title">
						电话
					</view>
					<view class="address_content">
						<input type="text" placeholder="请输入联系电话" v-model="phone" />
					</view>
				</view>
				<view class="main_item">
					<view class="address_title">
						地址
					</view>
					<view class="address_content_map" @click="openMap">
						{{address}}
					</view>
				</view>
				<view class="main_item">
					<view class="address_title">
						详细地址
					</view>
					<view class="address_content">
						<input type="text" placeholder="请输入详细地址" v-model="more_address" />
					</view>
				</view>
			</view>
			<view class="address_save_btn" @click="addAddress">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import commonHeader from '@/components/common-header/common_header_title.vue';
	export default {
		components:{
			commonHeader
		},
		data() {
			return {
				titleName:'添加地址',
				sex:-1,
				address:'请选择地址',
				more_address:'',
				username:'',
				phone:'',
				lon:'',//经度
				lat:''
			}
		},
		onLoad() {
			
		},
		methods: {
			chooseSex(type){
				this.sex = type
			},
			openMap(){
				// this.$uniNavigateTo('../../chooseAddress/chooseAddress')
				//小程序打开地图
				uni.getSetting({
				    success: (res) => {
						console.log(res)
				      if (res.authSetting['scope.userLocation']) { /* 用户授权成功时走这里 */
				        this.handerChooseLocation()
				      } else if (res.authSetting['scope.userLocation'] === undefined) { /* 用户未授权时走这里 */
				        this.handleOpenSetting()
				      } else { /* 用户拒绝了授权后走这里 */
				        this.handleOpenSetting()
				      }
				    }
				  })
			},
			handleOpenSetting () { /* 引导用户开启权限 */
			  uni.openSetting({
			    success: (res) => {
			      if (res.authSetting["scope.userLocation"]) {
			        this.handerChooseLocation()
			      }
			    }
			  })
			},
			handerChooseLocation () {
				let that = this
				uni.getLocation({
					type: 'wgs84',
					geocode:true,//设置该参数为true可直接获取经纬度及城市信息
					success: function (res) {
						uni.chooseLocation({
						  latitude: res.latitude || '', /* 纬度 */
						  longitude: res.longitude || '', /* 经度 */
						  success: (res) => {
								that.address = res.name
								// this.more_address = res.address
								that.lon = res.longitude
								that.lat = res.latitude
						  },
						  fail: function (err) {
						      console.log('取消按钮', err)
						  }
						})
					},
					fail: function () {
						uni.showToast({
							title: '获取地址失败，将导致部分功能不可用',
							icon:'none'
						});
					}
				});
			  
			},
			addAddress(){
				let that = this
				if(that.username == ''){
					this.$commonJs.showToast.showNoneToast('请输入联系人')
					return false
				}
				if(that.phone == ''){
					this.$commonJs.showToast.showNoneToast('请输入手机号')
					return false
				}
				if(!(this.$commonJs.check.telphone(that.phone))){
					return false;
				}
				if(that.address == ''){
					this.$commonJs.showToast.showNoneToast('请选择地址')
					return false
				}
				if(that.more_address == ''){
					this.$commonJs.showToast.showNoneToast('详细地址不能为空')
					return false
				}
				if(that.sex == -1){
					this.$commonJs.showToast.showNoneToast('请选择性别')
					return false
				}
				let sex_name = ''
				if(that.sex == 1){
					sex_name = '男'
				}else{
					sex_name = '女'
				}
				let postData = {contacts:that.username,contactPhone:that.phone,address:that.address,detailedAddress:that.more_address,longitude:that.lon,latitude:that.lat,sex:sex_name}
				this.$api.address.addAddress(postData)
				.then(res => {
					console.log(res)
					if(res.code == 200){
						this.$commonJs.showToast.showNoneToast(res.msg)
						uni.navigateBack({
							delta:1
						})
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			}
		}
	}
</script>
